var app = getApp();
var ux = require('../../../lib/ux.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowtime: null,
    yeList: [],
    orderId:0,
    merchant: {},
    merchantName: '',
    submitUrl: "/api/balance/carConfirmOrder.do",
    action: "/api/memberShop/orderProducsList.do",
    submit: "{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      nowtime: new Date().getTime(),
      yeList:JSON.parse(options.yeList),
      orderId: options.orderId,
      merchant: wx.getStorageSync("banrdId"),
      merchantName: wx.getStorageSync("banrd").brandName
    })
    var self = this;
    wx.showLoading({
      title: '加载中....',
      mask: true
    })
    ux.login(function (protocol) {
      if (protocol.success) {
        wx.setStorageSync("userInfo", protocol.data.userInfo);
        wx.setStorageSync("token", protocol.data.token);
        self.setData({
          userInfo: protocol.data.userInfo
        })
        self.getList();
      }
    })

  },

  onForm: function (event) {
    if (this.data.submit) {
      var $this = this;
      var dataset = event.currentTarget.dataset;
      ux.request(this.data.submit, detail.value, function (protocol) {
        if (protocol.success) {
          if (dataset.url) {
            wx.redirectTo(dataset.url);
          }
        }
      }, true);
    }
  },

  onTap: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val) {
      data[dataset.key] = dataset.val;
    }
    this.setData(data);
  },
  jianCount: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    var lists = this.data.list;
    if (lists[index].count == 0) {
      lists[index].select = 0;
      this.setData({
        list:lists
      })
      return false;
    } else {
     
      if (lists[index].select == 1) {
        lists[index].count = lists[index].count - 1;
        lists[index].number = lists[index].number + 1;
        this.setData({
          list: lists
        })
        if (lists[index].count == 0) {
          lists[index].select = 0;
          this.setData({
            list: lists
          })
        }
      }
     
    }
  },
  jiaCount: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    var lists = this.data.list;
    if (lists[index].count == lists[index].pLimit){
      this.toast("当前订单该商品购买数量为" + lists[index].pLimit);
       return false;
    }
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面

    if (prevPage.data.selectGood == lists[index].commodity && lists[index].count == lists[index].pLimit-1){
      this.toast("该商品已使用一张优惠券,余额最多使用" + Number(lists[index].pLimit - 1)+"张");
      return false;
    }
    lists[index].count = lists[index].count + 1;
    lists[index].select = 1;
    lists[index].number = lists[index].number-1;
     this.setData({
      list: lists
    })
  },
  selects: function (event) {
    var dataset = event.currentTarget.dataset;
    var index = dataset.index;
    var lists = this.data.list;
    if (lists[index].select == 1) {
      lists[index].select = 0
      this.setData({
        list: lists
      })
    } else {
      lists[index].select = 1
      this.setData({
        list: lists
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  getList: function () {
    var self = this;
    
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = { orderId: self.data.orderId};
    ux.request(self.data.action, params, function (data) {
      var listss = data.data.list;
      var counts= self.data.yeList;
      if (counts.length>0){
        for (var i = 0; i < counts.length; i++) {
          for (var a = 0; a < listss.length; a++) {
               if(counts[i].id == listss[a].id){
                  listss[a].select=1;
                  listss[a].count=counts[i].count;
               }
          }
        }
      }
     
      wx.hideLoading();
        self.setData({
          list: listss
        });
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      isShow: true
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  toGoconfirm: function () {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面
    var goods = prevPage.data.list;
    var lists=this.data.list;
    var yedi = 0;
    var yeList=[];
    for (var i = 0; i < lists.length; i++) {
      if (lists[i].count>0) {
        for (var a = 0; a < goods.length; a++) {
          if (lists[i].commodity == goods[a].goodsNumber){
              var item = {};
            item.commodity = lists[i].commodity;
              item.id = lists[i].id;
              item.count = lists[i].count;
              yeList.push(item);
            yedi = ux.acAdd(yedi, ux.acMul(lists[i].count, goods[a].price)) 
            }
        }
      }
    }
    prevPage.setData({
      sumff: ux.acSub(ux.acSub(ux.acAdd(prevPage.data.sumDouble, prevPage.data.peisong), yedi), prevPage.data.ydi),
      sum: ux.acSub(ux.acSub(prevPage.data.sumDouble, yedi),prevPage.data.ydi),
      pdi: yedi,
      yeList: yeList
    })
    wx.navigateBack({
      delta: 1
    })
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  }
})
